package band.wukong.practice.webservice.kh.sample12;

import band.wukong.practice.webservice.kh.sample11.LicenseHandler;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.xml.ws.handler.Handler;
import java.util.ArrayList;
import java.util.List;

/**
 * 通过interceptor增加header的例子
 *
 * cd D:\Work\Lab\bin-java\apache-cxf-3.1.11\bin
 * wsdl2java -d d:\ -encoding utf-8 -keep -verbose http://localhost:9000/wkp/webservice/kh/sample12?wsdl
 *
 * @author wukong(wukonggg@139.com)
 */
public class RobotServer {

    private static final Logger logger = LoggerFactory.getLogger(RobotServer.class);

    public static void main(String[] args) {
//        stringInInterceptor();
        objectInInterceptor();
        logger.info("Server started.");
        logger.info("WSDL = " + RobotConst.SERVICE_WSDL);
    }

    /**
     * header内容为string
     */
    private static void stringInInterceptor() {
        JaxWsServerFactoryBean  factory = new JaxWsServerFactoryBean();
        factory.setAddress(RobotConst.SERVICE_URL);
        factory.setServiceClass(RobotService.class);
        factory.setServiceBean(new RobotServiceImpl());

        factory.getInInterceptors().add(new LoggingInInterceptor());

        // 跟sample11一样用handler接收
        List<Handler> handlers = new ArrayList<>();
        handlers.add(new LicenseHandler());
        factory.setHandlers(handlers);

        factory.create();
    }

    /**
     * header内容为object
     */
    private static void objectInInterceptor() {
        JaxWsServerFactoryBean  factory = new JaxWsServerFactoryBean();
        factory.setAddress(RobotConst.SERVICE_URL);
        factory.setServiceClass(RobotService.class);
        factory.setServiceBean(new RobotServiceImpl());

        factory.getInInterceptors().add(new LoggingInInterceptor());
        factory.getInInterceptors().add(new ServerInboundObjectLicenseInterceptor());

        factory.create();
    }
}
